package main

import (
	"flag"
	"log"
	"time"

	_ "gitee.com/xiaoa7/jsetl/service"
	_ "gitee.com/xiaoa7/jsetl/service/console"       //控制台终端
	_ "gitee.com/xiaoa7/jsetl/service/elasticsearch" //es数据库
	_ "gitee.com/xiaoa7/jsetl/service/excel"         //excel数据文件操作
	_ "gitee.com/xiaoa7/jsetl/service/http"          //http api接口
	_ "gitee.com/xiaoa7/jsetl/service/mongo"         //mongo数据库
	_ "gitee.com/xiaoa7/jsetl/service/redis"         //redis
	_ "gitee.com/xiaoa7/jsetl/service/rmdb"          //rmdb所有关系型数据库
	jsvm "gitee.com/xiaoa7/jsvm-core"
)

var (
	act        = flag.String("act", "run", "要执行的动作[run|encrypt]")
	mainKey    = flag.String("mainkey", "main@123", "密钥1")
	subKey     = flag.String("subkey", "sub@123", "密钥2")
	fileKey    = flag.String("filekey", "123456789009876543211234567890ab", "文件密钥")
	jsFile     = flag.String("jsfile", "./redis.js", "js文件")
	dsFile     = flag.String("dsfile", "./redis.ds", "ds文件")
	runtimeout = flag.Int64("timeout", 30, "脚本运行超时【30秒】")
)

func init() {
	flag.Parse()
}

func main() {
	vm := jsvm.NewJsVM(*mainKey, *subKey, true)
	if *act == "run" {
		if err := vm.LoadJsFileWithTimeout(*jsFile, time.Duration(*runtimeout)*time.Second); err != nil {
			log.Fatal(err)
		}
	} else if *act == "encrypt" {
		vm.EncryptJsFile(*jsFile, *dsFile, *fileKey)
	}
}
